package com.liuchang.utils;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.liuchang.config.AppConfig;
import com.liuchang.exception.MyNullException;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * @author :liuchang
 * @description:TODO
 * @date :2021/8/15 19:29
 */
@Component
public class FastDfsClientUitl {

    @Autowired
    private FastFileStorageClient storageClient;

    @Autowired
    private AppConfig appConfig;

    /**
     * 上传文件
     * @param file 文件对象
     * @return 文件访问地址
     * @throws IOException
     */
    public String uploadFile(MultipartFile file) throws IOException {
        if (ObjectUtils.isEmpty(file)) {
            throw new MyNullException();
        }
        StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null);
        return getAccessUrl(storePath);
    }

    /**
     *
     * @param storePath
     * @return 返回文件的完整地址
     */
    private String getAccessUrl(StorePath storePath) {
        String fileUrl = "http://" + appConfig.getHost() + ":" + appConfig.getPort() + "/" + storePath.getFullPath();
        return fileUrl;
    }

    /**
     * 删除文件
     * @param fileUrl
     * @return
     */

    public String deleteFile(String fileUrl) {
        if (StringUtils.isNoneBlank(fileUrl)) {
            storageClient.deleteFile(fileUrl);
            return "文件删除成功";
        }
        return "文件路径不存在";
    }

    public void downLoad(String fileUrl, HttpServletResponse response) throws IOException {
        int portStart = fileUrl.lastIndexOf(":") + 1;
        int groupStart = fileUrl.indexOf("/", portStart) + 1;
        int groupeEnd = fileUrl.indexOf("/", groupStart);
        String group = fileUrl.substring(groupStart, groupeEnd);

        int fileNameStart = fileUrl.lastIndexOf("/") + 1;
        String fileName = fileUrl.substring(fileNameStart);

        String path = fileUrl.substring(groupeEnd + 1);

        byte[] bytes = storageClient.downloadFile(group, path, new DownloadByteArray());

        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        response.setCharacterEncoding("UTF-8");

        // 写入到流
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
            out.write(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
